package com.example.firstjavamqtt.service.log.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.firstjavamqtt.model.dto.log.TemperatureHumidityLogDto;
import com.example.firstjavamqtt.model.dto.mqtt.MqttTemperatureHumidityDto;
import com.example.firstjavamqtt.model.log.DeviceLog;
import com.example.firstjavamqtt.service.log.DeviceLogService;
import com.example.firstjavamqtt.service.log.TemperatureHumidityLogService;
import com.example.firstjavamqtt.common.excepiton.GlobalException;
import com.example.firstjavamqtt.common.result.ResultCodeEnum;
import com.example.firstjavamqtt.mapper.TemperatureHumidityLogMapper;
import com.example.firstjavamqtt.model.log.TemperatureHumidityMonitorLog;
import com.example.firstjavamqtt.utils.LogRequestUtil;
import com.example.firstjavamqtt.utils.RedisUtil;
import com.example.firstjavamqtt.utils.SqlUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;

@Service
public class TemperatureHumidityLogServiceImpl extends ServiceImpl<TemperatureHumidityLogMapper, TemperatureHumidityMonitorLog> implements TemperatureHumidityLogService {

    @Autowired
    RedisUtil redisUtil;
    @Autowired
    DeviceLogService deviceLogService;

    //保存
    @Override
    public void saveTemperatureHumidityLog(MqttTemperatureHumidityDto dto, HttpServletRequest request) {
        if (!dto.isOnline()) {
            throw new GlobalException(ResultCodeEnum.OFFLINE);
        }
        TemperatureHumidityMonitorLog temperatureHumidityMonitorLog = new TemperatureHumidityMonitorLog();
        BeanUtils.copyProperties(dto, temperatureHumidityMonitorLog);

        LogRequestUtil.setRequest(temperatureHumidityMonitorLog, request);
        System.out.println(temperatureHumidityMonitorLog);

        baseMapper.insert(temperatureHumidityMonitorLog);
    }

    //删除某个id的TemperatureHumidityLog
    @Override
    public void deleteTemperatureHumidityLogById(int id) {
        int i = baseMapper.deleteById(id);
        if (i < 1) {
            throw new GlobalException(ResultCodeEnum.PARAM_ERROR);
        }
    }

    //删除某个设备id的所有日志
    @Override
    public void deleteTemperatureHumidityLogByDeviceId(String deviceId) {
        int i = baseMapper.delete(getWrapperByDeviceId(deviceId));
        if (i < 1) {
            throw new GlobalException(ResultCodeEnum.PARAM_ERROR);
        }
    }

    //模糊查询
    @Override
    public Page<TemperatureHumidityMonitorLog> getLogByDto(TemperatureHumidityLogDto dto, int pageNum, int limit) {
        //System.out.println(dto.toString());
        Page<TemperatureHumidityMonitorLog> page = new Page<>(pageNum, limit);
        QueryWrapper<TemperatureHumidityMonitorLog> wrapper = new SqlUtil.Dynamic<TemperatureHumidityMonitorLog>()
                .like("device_id", dto.getDeviceId())
                .ge("operation_date", dto.getStartDate())
                .le("operation_date", dto.getEndDate())
                .ge("humidity", dto.getLowestHumidity())
                .le("humidity", dto.getHighestHumidity())
                .ge("temperature", dto.getLowestTemp())
                .le("temperature", dto.getHighestTemp())
                .build();
        return baseMapper.selectPage(page, wrapper);
    }

    //根据LedLog查询
    @Override
    public void deleteLedLogByBatchId(ArrayList<Integer> batchId) {
        int i = baseMapper.deleteBatchIds(batchId);
        if (i < 1) {
            throw new GlobalException(ResultCodeEnum.PARAM_ERROR);
        }
    }

    //根据设备ID获取温湿度
    @Override
    public Page<TemperatureHumidityMonitorLog> getByDeviceId(int pageNum, int limit, String deviceId,HttpServletRequest request) {
        //先刷一下redis
        MqttTemperatureHumidityDto dto = ((MqttTemperatureHumidityDto) redisUtil.get("TempHumidity:" + deviceId));
        if(dto!=null){
            this.saveTemperatureHumidityLog(dto,request);
            deviceLogService.saveDeviceLog(JSONObject.toJSONString(dto),request);
        }
        Page<TemperatureHumidityMonitorLog> page = new Page<>(pageNum, limit);
        return baseMapper.selectPage(page, getWrapperByDeviceId(deviceId));
    }

    @Override
    public Page<TemperatureHumidityMonitorLog> getLog(int pageNum, int limit) {
        Page<TemperatureHumidityMonitorLog> page = new Page<>(pageNum, limit);
        return baseMapper.selectPage(page, null);
    }

    private QueryWrapper<TemperatureHumidityMonitorLog> getWrapperByDeviceId(String deviceId) {
        return new QueryWrapper<TemperatureHumidityMonitorLog>().eq("device_id", deviceId);
    }
}
